<!-- 商品库存查询 -->
<template>
  <el-dialog title="商品查询" :visible.sync="visible" fullscreen center append-to-body>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-divider content-position="left">查询条件</el-divider>
    <el-form :inline="true" :model="dataForm">
      <el-form-item label="商品代码">
        <el-input v-model="dataForm.code"></el-input>
      </el-form-item>
      <el-form-item label="商品名称">
        <el-input v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="镜片材质" v-if="dataForm.ctgId == 3">
        <el-select v-model="dataForm.lensMaterial" clearable>
          <el-option v-for="item in bizData.opticMaterialData" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="镜片种类" v-if="dataForm.ctgId == 3">
        <el-select v-model="dataForm.photometricClassification" clearable>
          <el-option v-for="item in bizData.photometricClassification" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="折射率" v-if="dataForm.ctgId == 3">
        <el-select v-model="dataForm.refractiveIndex" clearable>
          <el-option v-for="item in bizData.refractivityData" :value="item.value" :label="item.label" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="销售价格">
        <el-row>
          <el-col :span="11">
            <el-form-item label-width="0">
              <el-input v-model="dataForm.goodsPriceMin"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="2" style="text-align: center;">至</el-col>
          <el-col :span="11">
            <el-form-item label-width="0">
              <el-input v-model="dataForm.goodsPriceMax"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item v-if="(dataForm.ctgId == 3 || dataForm.ctgId == 4) && dataForm.customizeType == '0'">
        <el-checkbox v-model="checked" :true-label="1" :false-label="0" @change="valueChangeHandle">是否展示0库存</el-checkbox>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain @click="getDataList">查询</el-button>
        <el-button type="info" plain @click="clearSearchForm">清空</el-button>
      </el-form-item>
    </el-form>
    <el-divider content-position="left">查询数据</el-divider>
<!--    <el-row style="margin-bottom: 5px;">
      <el-button type="primary" plain @click="selectAndClose">选择并关闭</el-button>
    </el-row> -->
    <div class="avue-crud__pagination" style="margin-bottom: 25px;">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100, 200]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next">
      </el-pagination>
    </div>
    <div class="avue-crud">
      <el-table :data="dataList" height="calc(100vh - 215px)" ref="goodsTable" border v-loading="dataListLoading">
        <el-table-column type="index" width="50">
          <template slot-scope="scope">
            <el-radio v-model="dataForm.goodsId" :label="scope.row.id" style="position: relative;left: 6px;top:-2px;" @change="checkItem(scope.row)">&nbsp;</el-radio>
          </template>
        </el-table-column>
        <!-- <el-table-column key="1" type="selection" width="45" :selectable="checkSelectable"></el-table-column> -->
        <el-table-column key="2" prop="code" label="商品代码"></el-table-column>
        <el-table-column key="3" prop="barcode" label="商品条码"></el-table-column>
        <el-table-column key="4" prop="name" label="商品名称"></el-table-column>
        <el-table-column key="5" prop="goodsPrice" label="零售价格"></el-table-column>
        <el-table-column key="6" prop="promotionPrice" label="促销价格"></el-table-column>

        <el-table-column key="7" prop="mirrorBall" label="球镜"></el-table-column>
        <el-table-column key="8" prop="columnMirror" label="柱镜"></el-table-column>
        <el-table-column key="9" prop="toTheLight" label="下加光" v-if="dataForm.ctgId == '3'"></el-table-column>
        <el-table-column key="10" prop="refractiveIndex" header-align="center" align="center" label="折射率" v-if="dataForm.ctgId == '3'"></el-table-column>
        <el-table-column key="11" prop="photometricClassification" header-align="center" align="center" label="镜片分类" v-if="dataForm.ctgId == '3'">
          <template slot-scope="scope">
            {{getValNameByType(bizData.photometricClassification,scope.row.photometricClassification)}}
          </template>
        </el-table-column>
        <el-table-column key="12" prop="lensMaterial" label="材料分类" v-if="dataForm.ctgId == '3'">
          <template slot-scope="scope">
            {{getValNameByType(bizData.lensMaterial,scope.row.lensMaterial)}}
          </template>
        </el-table-column>

        <el-table-column key="13" prop="curvature" label="曲率" v-if="dataForm.ctgId == '4'"></el-table-column>
        <el-table-column key="14" prop="diameter" label="直径" v-if="dataForm.ctgId == '4'"></el-table-column>


        <el-table-column key="15" prop="useType" label="使用类型" v-if="dataForm.ctgId == '4'">
          <template slot-scope="scope">
            {{getValNameByType(bizData.useType,scope.row.useType)}}
          </template>
        </el-table-column>
        <el-table-column key="16" prop="disusedClassification" label="抛弃类型" v-if="dataForm.ctgId == '4'">
          <template slot-scope="scope">
            {{getValNameByType(bizData.disusedClassification,scope.row.disusedClassification)}}
          </template>
        </el-table-column>


        <el-table-column key="17" prop="batch" label="批次" v-if="dataForm.ctgId == '4'"></el-table-column>
        <el-table-column key="18" prop="invalidStr" label="效期" v-if="dataForm.ctgId == '4'"></el-table-column>
        <el-table-column key="19" prop="stockName" label="仓位" v-if="dataForm.selfFlag != '1'"></el-table-column>
        <el-table-column key="20" prop="storageQuantity" label="数量" v-if="(dataForm.selfFlag != '1')"></el-table-column>
      </el-table>

    </div>

    <!-- <div class="avue-crud__pagination">
      <el-pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage" background layout="total, sizes, prev, pager, next, jumper"></el-pagination>
    </div> -->
  </el-dialog>
</template>

<script>
  import { filterParams,deduplicationBykey, deduplicationBykey1 } from "@/util/tools";
  import { remote } from '@/api/admin/dict'

  import { inventoryDetailFetchList, getGlassStorageSaleByPage, getGoodsStorage } from "@/api/eims/storage";
  import { goodsFetchList } from '@/api/eims/goods'
  import { mapGetters } from "vuex";
  export default {
    data() {
      return {
        visible: false,
        checked: false,
        frameMaterialData: [],
        accessoryTypeData: [],
        customizeType: '',
        paramsData: {},
        eyesType: 1,
        reserveSelection: true,
        dataForm: {
          ctgId: "",
          showZero: '0',
          mirrorBall: "", // 球镜
          columnMirror: "", // 柱镜
          customizedMirrorBallMax: "",
          customizedMirrorBallMin: "",
          customizedColumnMirrorMax: "",
          customizedColumnMirrorMin: "",
          customizeType: "0",
          opticMaterial: "",
          opticType: "",
          key: "",
          person: "",
          address: ""
        },
        rowItem: {},

        dataList: [],
        pageIndex: 1,
        pageSize: 100,
        totalPage: 0,
        dataListLoading: false,
        flag: true
      };
    },
    computed: {
      ...mapGetters(["permissions", "bizData"])
    },
    methods: {
      init(data) {
        this.pageIndex = 1
        this.customizeType = '0'
        this.params = data.params
        this.oldGoods = data.oldGoods
        for(let key in this.dataForm){
          if(key != 'ctgId'){
            this.dataForm[key] = ''
          }
        }
        this.checked = 0
        this.goodsId = ''
        this.dataForm.customizeType = "0";
        this.visible = true;
        this.getDataList();

      },
      //  清空搜索表单
      clearSearchForm() {
        for (let key in this.dataForm) {
          if(key == 'ctgId' || key == 'customizeType'){
          }else{
            this.dataForm[key] = "";
          }
        }
      },
      // 获取数据列表
      getDataList() {
        this.dataForm.ctgId = this.params.ctgId
        this.dataForm.stockId = 1
        this.dataForm.selfFlag = 0
        this.dataListLoading = true
        this.dataList = []
        this.dataForm.mirrorBall = this.params.ballGlass
        this.dataForm.columnMirror = this.params.postGlass
        let params1 = { current: this.pageIndex, size: this.pageSize }
        let params2 = filterParams(this.dataForm)
        switch(this.dataForm.ctgId){
          case 3:
            getGoodsStorage(Object.assign(params1, params2)).then(response => {
              this.dataListLoading = false
              if(response.data.code == '0'){
                this.dataList = response.data.data.records
                this.dataList.forEach(item => {
                  item.goodsId = item.id
                })
                this.totalPage = response.data.data.total
                this.customizeType = this.dataForm.customizeType
                this.$nextTick(() => {
                  for (let i = 0; i < this.dataList.length; i++) {
                    if (this.dataList[i].goodsId == this.oldGoods.goodsId) {
                      this.$refs.goodsTable.setCurrentRow(this.dataList[i])
                    }
                  }
                })
              }else{
                this.$message.error(response.data.msg)
              }
            })
            break;
          case 4:
            getGoodsStorage(Object.assign(params1, params2)).then(response => {
              if(response.data.code == '0'){
                this.dataList = response.data.data.records;
                this.dataList.forEach(item => {
                  item.goodsId = item.id
                })
                this.totalPage = response.data.data.total;
                this.customizeType = this.dataForm.customizeType
                this.$nextTick(() => {
                  for (let i = 0; i < this.dataList.length; i++) {
                    if (this.dataList[i].goodsId == this.oldGoods.goodsId && this.dataList[i].batch == this.oldGoods.batch && this.dataList[i].invalidTime == this.oldGoods.invalidTime) {
                      this.$refs.goodsTable.setCurrentRow(this.dataList[i])
                    }
                  }
                })
              }else{
                this.$message.error(response.data.msg)
              }
            })
          break;
        }
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val;
        this.pageIndex = 1;
        this.getDataList();
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val;
        this.getDataList();
      },

      checkItem(row){
        let newGoods = row
        newGoods.goodsQuantity = 1
        newGoods.discountRate = '1'
        newGoods.discountPrice = '0'
        newGoods.removePrice = '0'
        newGoods.refundPrice = '0'

        newGoods.id = this.oldGoods.id
        newGoods.glassFlag = this.oldGoods.glassFlag
        newGoods.goodsDescription = this.oldGoods.goodsDescription
        newGoods.goodsCode = row.code
        newGoods.goodsDisplayName = row.viewGoodsName
        newGoods.price = row.goodsPrice
        newGoods.originalPrice = row.goodsPrice
        newGoods.receivablePrice = row.goodsPrice
        newGoods.newGoods = true
        this.$set(newGoods,'outStockId',this.oldGoods.outStockId)

        this.visible = false
        this.$emit("refreshForm", {
          oldGoods: this.oldGoods,
          newGoods: newGoods
        });
      },
      handleSelectionChange(row) {
        console.log(row)
      },
      getValNameByType(data,type) {
        let obj = {}
        obj = data.find(item => {
          return item.value == type
        })
        if(obj && obj.label){
          return obj.label
        }
      },
      //  多选框值转换
      valueChangeHandle(val){
        if(val == '1'){
          this.$set(this.dataForm,'showZero','1')
        }else{
          this.$set(this.dataForm,'showZero','0')
        }
        this.dataForm = {...this.dataForm}
      },
    }
  };
</script>

<style lang="scss" scoped>
  .el-table .disabledCheck .cell .el-checkbox__inner {
    display: none !important;
  }
</style>
